// pages/goodsDetail/goodsDetail.js
const http = require('../../utils/http.js');
const app = getApp();
const { $Toast } = require('../../dist/base/index');
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },
  //获取商品详情
  getGoodsDetail() {
    var gid = this.data.gid;
    http.fetchApi('procedure_t', {
      p: 'R2006001' + '|' + gid
    }).then(res => {
      if (res.data.code == 1) {
        var prodectArr = res.data.results[0];
        this.setData({
          prodectArr
        })
      }
    })
  },

  //加入购物车
  addShopCar(e) {
    var pages = getCurrentPages();
    //上一个页面实例对象
    var prePage = pages[pages.length - 2];
    var gid = this.data.gid;
    if (prePage.route == 'pages/FlashDelivery/FlashDelivery') {
      var shopCar = wx.getStorageSync('shopCar') || [];
      var shopCared = shopCar.filter(item => item.gid == gid);
     
      if (shopCared.length > 0) {
       
        $Toast({
          content: '商品已存在购物车中',
          type: 'warning'
        });
        return
      }

    } else if (prePage.route == 'pages/FlashDelivery1/FlashDelivery1') {
      var shopCar1 = wx.getStorageSync('shopCar1') || [];
      var shopCared1 = shopCar1.filter(item => item.gid == gid);
      if (shopCared1.length > 0) {
        $Toast({
          content: '商品已存在购物车中',
          type: 'warning'
        });
        
        return
      }
    }
    prePage.addNum(e)
    $Toast({
      content: '成功加入购物车',
      type: 'success'
    });
  },

  //立即购买
  toBuy(e) {

    var gid = e.currentTarget.dataset.item.gid;
    var item = e.currentTarget.dataset.item;
    var pages = getCurrentPages();
    //上一个页面实例对象
    var prePage = pages[pages.length - 2];
    // prePage.addNum(e);
    // console.log(prePage.route);
    if (prePage.route == 'pages/FlashDelivery/FlashDelivery') {

    var shopCar = wx.getStorageSync('shopCar') || [];
     var index= shopCar.findIndex(item => item.gid == gid);
     if(index!=-1){
       //购物车中有此商品
       shopCar[index].is_select=true;
     }else{
       //购物车中无此商品
       item.goods_sl = 1;
       item.is_select = true;
       if (shopCar.length == 0) {

         shopCar.push(item);
       } else {


         var findSameId = shopCar.findIndex((i, d) => item.shid == i.shid);
         if (findSameId == -1) {
           wx.showModal({
             content: '你所购买的商品商家不统一，请将当前购物车商品进行结算后，再继续购物。',
             success: (res) => {
               if (res.confirm) {
                 wx.navigateTo({
                   url: '../shopCarDetail/shopCarDetail',
                 })
               } else if (res.cancel) {

               }
             }
           })

           return
         }else{
           var shopCar1 = shopCar.filter((item2, index) => {
             return item2.gid == item.gid
           })
           if (shopCar1.length == 0) {
             shopCar.push(item);
           } else {
             shopCar.forEach((item1, index) => {
               if (item1.gid == item.gid) {
                 item1.goods_sl++;
               }
             })
           }
         }

        
       }
     }
      wx.setStorageSync('shopCar', shopCar)

      wx.navigateTo({
        url: '../shopCarDetail/shopCarDetail',
      })
    } else if (prePage.route == 'pages/FlashDelivery1/FlashDelivery1') {

      var shopCar1 = wx.getStorageSync('shopCar1') || [];
      var index1 = shopCar1.findIndex(item => item.gid == gid);
      if (index1 != -1) {
        //购物车中有此商品
        shopCar1[index1].is_select = true;
      } else {
        //购物车中无此商品
        item.goods_sl = 1;
        item.is_select = true;
        if (shopCar1.length == 0) {

          shopCar1.push(item);
        } else {

          var findSameId = shopCar1.findIndex((i, d) => item.shid == i.shid);
          if (findSameId == -1) {
            wx.showModal({
              content: '你所购买的商品商家不统一，请将当前购物车商品进行结算后，再继续购物。',
              success: (res) => {
                if (res.confirm) {
                  wx.navigateTo({
                    url: '../shopCarDetail/shopCarDetail',
                  })
                } else if (res.cancel) {

                }
              }
            })

            return
          }else{
            var shopCar2 = shopCar1.filter((item2, index) => {
              return item2.gid == item.gid
            })
            if (shopCar2.length == 0) {
              shopCar1.push(item);
            } else {
              shopCar1.forEach((item1, index) => {
                if (item1.gid == item.gid) {
                  item1.goods_sl++;
                }
              })
            }
          }

         
        }
      }
      wx.setStorageSync('shopCar1', shopCar1)

      wx.navigateTo({
        url: '../shopCarDetail1/shopCarDetail1',
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var gid = options.gid;
    console.log(gid)
    this.setData({
      gid
    })

    this.getGoodsDetail();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})